package com.example.dell.intelligentagriculture.fragment;



import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;


import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dell.intelligentagriculture.R;
import com.example.dell.intelligentagriculture.utils.Agriculture;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.BreakIterator;


/**
 * A simple {@link Fragment} subclass.
 */
public class Co2Fragment extends Fragment {
    private TextView co2SetValue,co2CurrentValue;
    private View view;

    public Co2Fragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view==null) {
            view = inflater.inflate(R.layout.fragment_co2, null);
            co2CurrentValue = view.findViewById(R.id.co2CurrentValue);
            TextView co2CurrentValue = view.findViewById(R.id.co2CurrentValue);

            SharedPreferences sp = getActivity().getSharedPreferences("maxInt", Context.MODE_PRIVATE);
            String extreme = sp.getString("extreme", "");
            JSONObject json = null;
            try {
                json = new JSONObject(extreme);
                int minCo2 = json.getInt("minC02");
                int maxCo2 = json.getInt("maxCO2");
                co2SetValue.setText(minCo2+"~"+maxCo2);
            }catch (JSONException e){
                e.printStackTrace();
            }

        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent!=null){
            parent.removeView(view);
        }
        return view;
    }
    public void getAgrculture(Agriculture agriculture){
        co2CurrentValue.setText(agriculture.getCo2()+"");
    }
}
